<template>
	<div>
		<div v-if="loaded">
			<ab-custom-form></ab-custom-form>
		</div>
		<tabbar style="position:fixed">
			<tabbar-item>
				<span slot="icon" class="icon-primary-fa fa fa-clipboard" v-on:click="saveData"></span>
		 		<span slot="label" v-on:click="saveData">保存</span>
			</tabbar-item>
		</tabbar>	
  	</div>
</template>

<script>
import Vue from 'vue'
import bpmService from '@/service/bpm/bpmService'
import formService from '@/service/form/formService'

export default {
  	methods: {
  		saveData : function() {
			var errorMsg = formService.getValidateMsg(this.$children[0]);
			if(errorMsg){
				$.Dialog.alert("表单校验不通过！"+errorMsg,7);
				return; 
			}
			var url = Vue.__ctx + "/form/formDefData/saveData?key="+this.$route.query.key;
			var post = Vue.baseService.post(url, this.$children[1].$data.data);
			Vue.tools.getResultMsg(post);
		}
  	},
  	data :function () {
	    return {
	    	loaded : false
	    }
	},
  	created : function(){
  		var that = this;
  		var url = Vue.__ctx + "/form/formDefData/getData";
		var post = Vue.baseService.postForm(url, {
			key : this.$route.query.key,
			id : this.$route.query.id
		});
		Vue.tools.getResultData(post,function(data){
			var html = data.html;
			window.custFormComponentMixin = {};
			var custComponentData = data;
				
			// 处理 custFormComponentMixin
			if(html.indexOf("<script>") != -1){
				var reg=/<script[^>]*?>[\s\S]*?<\/script>/i;
				var patten = reg.exec(html)	
				var script = patten[0];
				
				var len1=script.indexOf(">")
				var len2=script.lastIndexOf("<")
				var custScriptStr = script.substring(len1+1,len2);
				html = html.replace(reg,"");
				
				if(custScriptStr){
					try{
						// TODO严格模式下问题
						eval(custScriptStr);// eslint-disable-line
					}catch(e){
						console.error("解析表单 JavaScript脚本 异常",custScriptStr,e);
					}
				}
				
				// 用于第三层子表数据暂存 _temp.xxx = [] //则该子表的数据暂存
				custComponentData.subTempData = {};
			}
			
			/**
			 * 公共的常用 表单 js
			 */
			var custFormMethods = {
				// 展示子表 TODO 改成组件 而不是subTempadata，subTableDialog这种，并添加确认框
				showSubTable: function(dataScope,tableName){
					if(!dataScope[tableName+"List"]){
						this.$set(dataScope,tableName+"List",[]);
					}
					
					this.$set(this.subTempData,tableName+"List",dataScope[tableName+"List"]);
					this.$set(this.subTableDialog,tableName,true);
				}
			};
			
			Vue.component('ab-custom-form',{
		    	name:"abCustomForm",
				mixins: [custFormComponentMixin],
				template: html,
				data:function(){
					return custComponentData;
				},
				methods:custFormMethods,
				mounted: function () {
				}
			});
			
			that.$data.loaded = true;
		});
		
  	}
}
</script>

<style scoped>

</style>
